package cn.xz.gobang.dialog

import android.view.Gravity
import android.widget.TextView
import cn.xz.gobang.R
import cn.xz.gobang.base.BaseDialogRxFragment
import cn.xz.gobang.util.GetDataUtils
import com.airbnb.lottie.LottieAnimationView

/**
 * 获胜弹窗
 * */
class WinDialog: BaseDialogRxFragment() {

    private var tvTitle: TextView? = null
    private var tvTxt: TextView? = null
    private var tvOver: TextView? = null
    private var lot1: LottieAnimationView? = null
    private var lot2: LottieAnimationView? = null

    private var winType: Int = -1
    private var mType: Int = -1

    companion object {
        fun newInstance(
            winType: Int,
            mType: Int
        ): WinDialog {
            val dialog = WinDialog()
            dialog.winType = winType
            dialog.mType = mType
            return dialog
        }
    }

    override fun getLayoutId(): Int {
        return R.layout.item_win_dialog
    }

    override fun configDialog() {
        this.setDialogSize(gravity = Gravity.CENTER,width = 300, height = 200)
    }

    override fun initViews() {

        dialog?.let {
            tvTitle = it.findViewById(R.id.tv_title)
            tvTxt = it.findViewById(R.id.tv_txt)
            tvOver = it.findViewById(R.id.tv_over)
            lot1 = it.findViewById(R.id.lot_1)
            lot2 = it.findViewById(R.id.lot_2)
        }

        //点击外部不隐藏
        dialog?.setCancelable(false)
        dialog?.setCanceledOnTouchOutside(false)
        //视图相关
        initUI()
        //点击事件
        initEvent()
    }

    private fun initUI(){
        var mLot1 = "lottie/lot_win.json"
        var mLot2 = "lottie/lot_win1.json"
        if (mType != -1){
            if (winType == mType){
                tvTxt?.text = "我方获胜"
                //我的积分+1
                GetDataUtils.setUserPart(true,1)
            }else{
                tvTxt?.text = "我方失败"
                mLot1 = "lottie/lot_lose1.json"
                mLot2 = "lottie/lot_lose.json"
                //我的积分-1
                GetDataUtils.setUserPart(false,1)
            }
        }else{
            if (winType == 0){
                tvTxt?.text = "黑棋获胜"
            }else{
                tvTxt?.text = "白旗获胜"
            }
        }
        lot1?.setAnimation(mLot1)
        lot1?.playAnimation()

        lot2?.setAnimation(mLot2)
        lot2?.playAnimation()
    }

    private fun initEvent(){
        if (tvOver != null){
            tvOver?.setOnClickListener {
                lot1?.clearAnimation()
                lot2?.clearAnimation()
                dismiss()
            }
        }
    }

}